﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class pay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Remove("payment_id");
            string chk = @"select *
                        from Table_payment
                        inner join Table_Sale
                            on Table_payment.Sale_id = Table_Sale.Sale_id
                        inner join Table_SaleDetail
                            on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
                        inner join Table_Around
                            on Table_Sale.Around_id = Table_Around.Around_id
                        inner join Table_Route
                            on Table_Around.Route_id = Table_Route.Route_id
                        inner join Table_Promotion
                            on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
                        inner join Table_Time
                            on Table_Around.Time_id = Table_Time.Time_id
                        inner join Table_Passenger
                            on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                        inner join Table_Category
                            on Table_Passenger.Category_id = Table_Category.Category_id
                        where Table_payment.payment_id = '" + TextBox1.Text + "'";
            try
            {
                SqlCommand sqlcomm = new SqlCommand();
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da = new SqlDataAdapter(chk, sqlconn);                
                DataTable dt = new DataTable();                
                da.Fill(dt);
                if (dt.Rows.Count <= 0)
                {
                    Response.Write(@"<script language='javascript'>alert('ไม่มีข้อมูลในระบบ');</script>");
                }
                else
                {
                    TextBox2.Text = dt.Rows[0]["payment_id"].ToString();
                    Session["payment_id"] = dt.Rows[0]["payment_id"].ToString(); 
                    float count = float.Parse(dt.Rows.Count.ToString());
                    TextBox3.Text = dt.Rows[0]["Route_source"].ToString() + "-" + dt.Rows[0]["Route_destination"].ToString();
                    TextBox7.Text = dt.Rows.Count.ToString();
                    string[] stempb = dt.Rows[0]["Around_date"].ToString().Split(' ');
                    string date = stempb[0] + " " + dt.Rows[0]["Time_time"].ToString();
                    TextBox4.Text = date;
                    //TextBox5.Text = dt.Rows[0]["Time_time"].ToString();                    
                    float Price = Int32.Parse(dt.Rows[0]["Price_ticket"].ToString());
                    float tex = Int32.Parse(dt.Rows[0]["tex"].ToString());
                    float promotion = float.Parse(dt.Rows[0]["Promotion_discount"].ToString());
                    float user = float.Parse(dt.Rows[0]["Category_Discount"].ToString());
                    float sumPro = (Price * (promotion + user)) / 100;
                    float sumall = (count * ((Price - sumPro) + tex));
                    TextBox6.Text = sumall.ToString("0.00");                    
                }
                
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("pay.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            string payment_id = Session["payment_id"].ToString();
            try
            {
                var q = from p in db.Table_payments
                        where p.payment_id == payment_id
                        select p;
                q.ToList().ForEach(c => c.payment_status = true);
                db.SubmitChanges();
                Response.Redirect("pay.aspx");               
            }
            catch (Exception)
            {               
                throw;
            }        
        }
    }
}